/**
 * 工具类
 * @type {{toMapList: util.toMapList, toMap: util.toMap}}
 */
import Vue from 'vue'
import File from "@/utils/File";
import * as global from "./global";
import XLSX from 'xlsx'

const util = {
  toMap: function (arr, idField) {
    if (!arr) return {};

    var map = {};
    arr.forEach(function (item) {
      var field = item[idField];
      map[field] = item;
    });
    return map;
  },

  toMapList: function (arr, idField, sortField) {
    if (!arr) return {};

    var map = {};
    arr.forEach(function (item) {
      var field = item[idField];
      let list = [];
      if (map[field]) {
        list = map[field];
      }
      list.push(item);
      if (sortField && list.length >= 2) {
        list.sort(function (a, b) {
          return (a[sortField] < b[sortField]) ? 1 : -1;
        });
      }
      map[field] = list;
    });
    return map;
  },


  // 上移、下移
  changeSortIndex: function (type, list, index) {
    var changeItemList = [];
    if (type == 1) {
      if (index == 0) return;

      list[index - 1].sortIndex++;
      list[index].sortIndex--;

      var temp = list[index - 1];
      Vue.set(list, index - 1, list[index])
      Vue.set(list, index, temp)

      changeItemList = [
        {
          id: list[index - 1].id,
          sortIndex: list[index - 1].sortIndex
        },
        {
          id: list[index].id,
          sortIndex: list[index].sortIndex
        }
      ];

    } else if (type == 2) {
      if (index == list.length - 1) return;
      list[index + 1].sortIndex--;
      list[index].sortIndex++;

      var temp = list[index + 1];
      Vue.set(list, index + 1, list[index])
      Vue.set(list, index, temp)

      changeItemList = [
        {
          id: list[index + 1].id,
          sortIndex: list[index + 1].sortIndex
        },
        {
          id: list[index].id,
          sortIndex: list[index].sortIndex
        }
      ];
    }

    if (changeItemList.length == 0) return;
  },

  getApiUrl: function (method, param, api) {
    var url;
    if (method && (method.indexOf("http://") >= 0 || method.indexOf("https://")) >= 0) {
      url = method;
    } else {
      if (!api && !method) return "";
      url = (api ? api : global.baseUrl).replace("{method}", method);
    }

    if (param) {
      if (url.indexOf("?") < 0) url += "?";
      for (var key in param) {
        url += "&" + key + "=" + param[key];
      }
    }
    return url;
  },

  doUploadFile: function (data, accept, maxSize, callBack, maxCount) {
    File.upload({
      multi: true,
      url: this.getApiUrl("/main/uploadFile"),
      param: data,
      accept: accept,
      before: function (files) {
        if (!maxSize) {
          if (files[0].size > maxSize * 1024) {
            Util.info("图像大小不能超过" + maxSize + "KB", 2);
            return false;
          }
        }

        if (maxCount) {
          if (files.length > maxCount) {
            Util.info("最多选择" + maxCount + "张图片");
            return false;
          }
        }
        Util.showLoading();
      },
      after: function (data) {
        Util.hideLoading();
        data = JSON.parse(data).data;
        callBack && callBack(Array.isArray(data) ? data[0] : data);
      },
      error: function () {
        Util.hideLoading();
        Util.info("上传失败", 3);
      },
      progress: function () {
      }
    });
  },

  XLSXRender(data, accept, maxSize, callBack, maxCount) {
    File.upload({
      multi: true,
      accept: accept,
      uploadType: "local",
      before: function (files) {},
      after: function (file) {
        var reader = new FileReader();
        reader.onload = function (e) {
          var data = e.target.result;
          var workbook = XLSX.read(data, {type: 'binary'});
          var sheetNames = workbook.SheetNames; // 工作表名称集合
          var worksheet = workbook.Sheets[sheetNames[0]]; // 这里我们只读取第一张sheet
          callBack && callBack(XLSX.utils.sheet_to_json(worksheet));
        };
        reader.readAsBinaryString(file);
      },
      error: function () {},
      progress: function () {}
    });
  }
}

export default util
